https://leetcode.cn/problems/4sum/submissions/
18. 四数之和

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        sort(nums.begin(),nums.end());
        vector<vector<int>> vv;
        int n=nums.size();
        for(int i=0;i<n;)
        {
            for(int j=i+1;j<n;) //三数之和为target-[i]
            {
                long long tar=(long long)target-nums[i]-nums[j];
                int left=j+1,right=n-1;
                while(left<right)    //俩数之和为target-i-j
                {
                    int sum=nums[left]+nums[right];
                    
                    if(sum<tar)
                        ++left;

                    else if(sum>tar)
                        --right;
                   
                    else{
                        vv.push_back({nums[i],nums[j],nums[left],nums[right]});
                        ++left,--right;
                        while(left<right&&nums[right]==nums[right+1]) --right;
                        while(left<right&&nums[left]==nums[left-1]) ++left;
                    }
                }
                ++j;
                while(j<n&&nums[j]==nums[j-1])
                    ++j;
            }
            ++i;
            while(i<n&&nums[i]==nums[i-1])
                ++i;
        }
        return vv;
    }
};